//
//  BaseObject.swift
//  TapTapSpeak
//
//  Created by liyahao on 2025/8/6.
//

import UIKit

public struct BaseModel<Model: Codable>: JSONable {
    var rhagit: Int
    var herbive: String
    var acrmost: Model?
    
    var success: Bool { rhagit == 0 }
}


public struct BaseObjct<Base> {
    public let base: Base
    
    init(_ base: Base) {
        self.base = base
    }
}

struct WapperModel<T>: Identifiable {
    var id = UUID()
    var t: T
}

/// 弱引用对象，因为数组会对元素强引用导致关联对象不释放内存
public final class WeakBox<T: NSObject> {
    public weak var weakObjc: T?
    public init(_ objc: T) {
        weakObjc = objc
    }
}

public protocol BaseCompatible {
    associatedtype CompatibleBase
    typealias CompatibleType = CompatibleBase
    static var tt: BaseObjct<CompatibleBase>.Type { get set }
    var tt: BaseObjct<CompatibleBase> { get set }
}

/// 基础协议扩展
extension BaseCompatible {
    public static var tt: BaseObjct<Self>.Type {
        get { return BaseObjct<Self>.self }
        set { }
    }
    
    public var tt: BaseObjct<Self> {
        get { return BaseObjct(self) }
        set { }
    }
}

// 让所有NSObject类都遵循基础协议，使基层自NSObject类都有lel属性
extension NSObject: BaseCompatible { }

/// 获取对象地址指针
public func objIdentifier(_ target: NSObject) -> UnsafeRawPointer {
    let targetIdentifier = ObjectIdentifier(target)
    let integerIdentifier = Int(bitPattern: targetIdentifier)
    return UnsafeRawPointer(bitPattern: integerIdentifier)!
}
